import modelExtend from 'dva-model-extend'
import { query, receipt } from './services/financialPaymentBill'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';

export default modelExtend(lwjPageModel, {
  namespace: 'financialPaymentBill',
  state: {
    currentItem: {},
    selectRows: [],
    pageOptions: {
      sort: 'createTime',
      order: 'DESC'
    },
    isExpand: false,
    queryParam: {},
    paymentTypeModal: false,
    paymentType: "",
    currentType:"SALES"
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/financialPaymentBill') {
          dispatch({ type: 'setCurrentTab', payload: "SALES" })
          dispatch({ type: 'query', payload: {} })
        }
      })
    }
  },
  effects: {
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ financialPaymentBill }) => financialPaymentBill.pageOptions);
      const queryParam = yield select(({ financialPaymentBill }) => financialPaymentBill.queryParam);
      const currentType = yield select(({ financialPaymentBill }) => financialPaymentBill.currentType);

      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,
          accountType:currentType
        }
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    /**
     * 查看详情
     */
    * showDetail({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/financialPaymentBill/detail/${payload.recordId}`));
    },
    /**
     * 审核付款单
     */
    * auditPaymentBill({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/financialPaymentBill/audit/${payload.recordId}`));
    },
    /**
     * 支付付款单
     */
    * payBill({
      payload
    }, { select, call, put }) {
      if (payload.paymentType === "OFFLINE") {
        yield put({ type: 'hidePaymentTypeModal', payload: {} })
        yield put(routerRedux.push(`/financialPaymentBill/doPayBill/${payload.paymentBillId}`));
      }
    },
    /**
     * 批量付款
     */
    * batchPayment({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push('/financialPaymentBill/doBatchPayment',
        { paymentBills: payload.selectRows }));
    },
    /**
     * 返充收款单
     */
    * receiptBill({
      payload
    }, { select, call, put }) {
      const data = yield call(receipt, payload.paymentBillId);
      yield put({ type: 'query', payload: {} })
    }
  },
  reducers: {
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    showPaymentTypeModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        paymentTypeModal: true
      }
    },
    hidePaymentTypeModal(state, { payload }) {
      return {
        ...state,
        paymentTypeModal: false,
        paymentType: ""
      }
    },
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
  }
})
